from sklearn import linear_model

reg = linear_model.LinearRegression()

reg.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
print(reg.coef_)


import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-3, 3, size=100)
X = x.reshape(-1, 1)
y = 0.5 * x**2 + x + np.random.normal(0, 1, 100)
print(X)

from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2)
poly.fit(X)
X2 = poly.transform(X)

print(X2.shape)
# 输出：(100, 3)

print(X2[:5, :])



lin_reg2 = linear_model.LinearRegression()
lin_reg2.fit(X2, y)
y_predict2 = lin_reg2.predict(X2)

plt.scatter(x, y)
plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='r')
plt.savefig('example_06_06.png')

from sklearn import svm 
X = [[0, 0], [1, 1]]

y = [0, 1]
clf = svm.SVC(gamma='scale')
clf.fit(X, y)
print(clf.predict([[2., 2.]]))

clf2 = svm.SVR()
clf2.fit(X, y)
print(clf2.predict([[2., 2.]]))

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB

iris = datasets.load_iris()
gnb = GaussianNB()
y_pred = gnb.fit(iris.data, iris.target).predict(iris.data)
print("Number of mislabeled points out of a total %d points : %d"
      % (iris.data.shape[0], (iris.target != y_pred).sum()))



from sklearn import tree


clf = tree.DecisionTreeClassifier()
clf.fit(X,y)

print(clf.predict([[2., 2.]]))



clf2 = tree.DecisionTreeClassifier()
clf2.fit(iris.data, iris.target)
#import graphviz
#dot_data = tree.export_graphviz(clf2, out_file=None)
#graph = graphviz.Source(dot_data)
#graph.render("iris")


from sklearn.cluster import KMeans, DBSCAN
from sklearn import metrics

X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0, n_init='auto').fit(X)
print(kmeans.labels_)

db = DBSCAN(eps=2, min_samples=3).fit(X)
print(db.labels_)



from sklearn.model_selection import train_test_split, cross_val_score

print(iris.data.shape, iris.target.shape)

x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=0)

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

clf = svm.SVC(kernel='linear',C=1).fit(x_train, y_train)
print(clf.score(x_test, y_test))



clf = svm.SVC(kernel='linear',C=1)
scores = cross_val_score(clf, iris.data, iris.target, cv=5)
print(scores)

